package com.gkzf.ai.module.crm.controller.admin.simpleCredit;

import com.gkzf.ai.framework.common.pojo.CommonResult;
import com.gkzf.ai.framework.common.pojo.PageResult;
import com.gkzf.ai.framework.common.util.object.BeanUtils;
import com.gkzf.ai.module.crm.controller.admin.simpleCredit.vo.CreditReportSimplePageReqVO;
import com.gkzf.ai.module.crm.controller.admin.simpleCredit.vo.CreditReportSimpleRespVO;
import com.gkzf.ai.module.crm.controller.admin.simpleCredit.vo.CreditReportSimpleSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.CreditReportDO;
import com.gkzf.ai.module.crm.dal.dataobject.creditreportsimple.CreditReportSimpleDO;
import com.gkzf.ai.module.crm.service.creditreportsimple.admin.CreditReportSimpleService;
import com.gkzf.ai.module.crm.service.creditreportsimple.app.AppCreditReportSimpleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.gkzf.ai.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - CRM 征信报告简版")
@RestController
@RequestMapping("/crm/credit-report-simple")
@Validated
public class CreditReportSimpleController {

    @Resource
    private CreditReportSimpleService creditReportSimpleService;

    @Resource
    private AppCreditReportSimpleService appCreditReportSimpleService;


    @PostMapping("/create")
    @Operation(summary = "创建CRM 征信报告简版")
    public CommonResult<Boolean> createCreditReportSimple(@Valid @RequestBody CreditReportSimpleSaveReqVO createReqVO) {
        CreditReportDO creditReportDO = new CreditReportDO();
        CreditReportSimpleDO creditReportSimple = appCreditReportSimpleService.createCreditReportSimple(createReqVO);
        appCreditReportSimpleService.createCreditReportSimple(createReqVO,creditReportSimple, creditReportDO);
        return success(true);
    }
    @GetMapping("/getCreditAnalysis")
    @Operation(summary = "简版征信分析结果")
    @Parameter(name = "simpleId", description = "小程序用户标识", required = true)
    public CommonResult<Object> getCreditAnalysis(@RequestParam("simpleId") Long simpleId) {
        CreditReportSimpleRespVO creditReportSimple = appCreditReportSimpleService.getCreditReportSimple(simpleId);
        String creditReport = appCreditReportSimpleService.analysisCreditReport(BeanUtils.toBean(creditReportSimple, CreditReportSimpleSaveReqVO.class));
        return success(creditReport);
    }


    @GetMapping("/get")
    @Operation(summary = "获得CRM 征信报告简版详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:credit-report-simple:details')")
    public CommonResult<CreditReportSimpleRespVO> getCreditReportSimple(@RequestParam("id") Long id) {
        CreditReportSimpleRespVO creditReportSimple = creditReportSimpleService.getCreditReportSimple(id);
        return success(creditReportSimple);
    }



    @GetMapping("/allocation")
    @Operation(summary = "分配CRM 征信报告简版")
    @Parameter(name = "userId", description = "分配的用户ID", required = true, example = "1024")
    @Parameter(name = "id", description = "简版ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:credit-report-simple:allocation')")
    public CommonResult<CreditReportSimpleRespVO> allocation(@Valid @RequestBody CreditReportSimpleSaveReqVO updateReqVO) {
        creditReportSimpleService.allocation(updateReqVO);
        return success(0);
    }

    @GetMapping("/page")
    @Operation(summary = "获得CRM 征信报告简版分页")
    @PreAuthorize("@ss.hasPermission('crm:credit-report-simple:query')")
    public CommonResult<PageResult<CreditReportSimpleRespVO>> getCreditReportSimplePage(@Valid CreditReportSimplePageReqVO pageReqVO) {
        PageResult<CreditReportSimpleRespVO> pageResult = creditReportSimpleService.getCreditReportSimplePage(pageReqVO);
        return success(pageResult);
    }


}